package com.itheima.service.impl;

import com.itheima.domain.Comment;
import com.itheima.domain.CommentExample;
import com.itheima.mapper.CommentMapper;
import com.itheima.service.CommentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class CommentServiceImpl implements CommentService {

    @Autowired
    private CommentMapper commentMapper;

    @Override
    public List<Comment> findCommentListByMoviesId(Integer moviesId) {
        // 1.创建查询对象
        CommentExample commentExample = new CommentExample();
        commentExample.createCriteria().andMoviesIdEqualTo(moviesId); // where movies_id = ?
        commentExample.setOrderByClause("time desc"); // order by time desc
        // 2.查询list，并返回
        return commentMapper.selectByExample(commentExample);
    }

    @Override
    public void saveComment(Comment comment) {
        //  1.完善信息
        comment.setCreatorName("匿名");
        comment.setTime(new Date());

        // 2.调用mapper保存
        commentMapper.insertSelective(comment);
    }
}
